// This is the "development time" runtime.js module
//
// This module can be replaced with another module called runtime.js by gelatin
// itself.

var oil = require('oil'),
    log = require('oil/log'),
    path = require('oil/path');

// Test if we're running inside the archive
if (typeof __V_INSIDE_ARCHIVE === 'undefined') {

  // Returns a handle to a file system-like object
  exports.dataTree = function(argv0) {
    // This assumes the layout of a Narwhal package.  argv0 is assumed to be
    // something like package-root/bin/gelatin, and then we want to access
    // package-root/data.
    var basePath = path.normalize(argv0, '..', '..', 'data');
    log.info('BASE PATH %s', basePath);
    return new oil.FileSystem(basePath);
  };

} else {

  exports.dataTree = function() {
    // For now, there is only one embedded tree per binary
    if (typeof __D_EMBEDDED_TREE === 'undefined') {
      throw {
        name: 'GelatinRuntimeError',
        message: 'This archive was built without __D_EMBEDDED_TREE'
      };
    }
    return new oil.GelatinEmbeddedTree(__D_EMBEDDED_TREE);
  };

}
